package com.hzpd.hainan.custorm;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.hzpd.hainan.R;
import com.hzpd.hainan.model.SuperEntity;
import com.hzpd.hainan.model.base.BaseEntity;
import com.hzpd.hainan.model.news.NewsTagBean;
import com.hzpd.hainan.model.news.NewsTagEntity;
import com.hzpd.hainan.model.video.VideoItenEntity;
import com.hzpd.hainan.module.news.adapter.NewsCancelGridAdapter;
import com.hzpd.hainan.net.Factory;
import com.hzpd.hainan.utils.DeviceUtils;
import com.hzpd.hainan.utils.LogUtils;
import com.hzpd.hainan.utils.SPUtil;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListAdapter;


import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;


public class NewsCancelDialog extends Dialog implements View.OnClickListener {

	private static View view;
	private static NewsCancelGridAdapter adapter;
	private Activity activity;
	private int x;
	private int y;
	private String nid;
	private GridView cancelGridView;
	private HashMap<Integer, Boolean> choiceMap = new HashMap<Integer, Boolean>();
	private List<String> select;
	private List<NewsTagBean>list;
	private String selectList;
	private Button news_cancel_bt;
	protected SPUtil spu;//

	public NewsCancelDialog(Context context,int x,int y,String nid) {
		super(context);
		activity = (Activity) context;
		this.x = x;
		this.y = y;
		this.nid = nid;
	}

	public NewsCancelDialog(Context context, int theme, int x,int y,String nid) {
        super(context, theme);
        activity = (Activity) context;
		this.x = x;
		this.y = y;
		this.nid = nid;
		spu = SPUtil.getInstance();
    }

	public NewsCancelDialog(Context context, int theme,String nid) {
        super(context, theme);
        activity = (Activity) context;
        this.nid = nid;

    }
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		init(x, y);	
		getNewsTagList();
	}	
	public void  init(int x, int y) {
		WindowManager wm = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
		int width = wm.getDefaultDisplay().getWidth();
		LogUtils.i("width-->"+width);
		int hight = wm.getDefaultDisplay().getHeight();
		if (y<hight*2/3) {
			view = LayoutInflater.from(activity).inflate(R.layout.newscancel_layout,null);
		}else {
			view = LayoutInflater.from(activity).inflate(R.layout.newscancel_layout,null);
		}
		setContentView(view);	
		cancelGridView = (GridView) view.findViewById(R.id.news_cancel_gridview);
		news_cancel_bt = (Button) view.findViewById(R.id.news_cancel_bt);
		news_cancel_bt.setOnClickListener(this);
		adapter = new NewsCancelGridAdapter(activity);
		cancelGridView.setAdapter(adapter);
		Window dialogWindow = getWindow();
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();		
		dialogWindow.setGravity(Gravity.RIGHT|Gravity.TOP);		
		//	 Gravity.RIGHT|Gravity.TOP 原点为右上角 x y 为向 X轴负方向 Y轴负方向的偏移量   这里如果想让dialog显示在按钮上方，Y轴的偏移量
		//  应该是 y- dialog的高度  这里根据判断按钮位置 来设置 dialog到底是在按钮上面还是下面
		//按钮的高度
		lp.x = (int) DeviceUtils.dp2px(activity.getResources(), 10);
		//设置 dialog的宽为window的0.8倍 高为0.35
		lp.width = (int) (width*0.95);

		//客户不喜欢弹框位置变换，所以改为固定某位置显示
		lp.y = hight*1/2;
		dialogWindow.setAttributes(lp);	
		select = new ArrayList<>();
		cancelGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				NewsTagBean ntb = (NewsTagBean) adapter.getItem(position);
				notLikeNews(ntb.getTagid());
				dismiss();
			}
		});
		
	}
		
	private void getNewsTagList() {
		Map<String, String> map = new HashMap();
		map.put("nid", nid);
		Factory.provideHttpService().newsLoseInterestTags(map)
				.subscribeOn(Schedulers.io())
				.observeOn(AndroidSchedulers.mainThread())
				.subscribe(new Action1<NewsTagEntity>() {
					@Override
					public void call(NewsTagEntity entity) {
						list =entity.data.getTags();
						adapter.appendData(list, true);
						adapter.notifyDataSetChanged();

						for (int i = 0; i < list.size(); i++) {
							choiceMap.put(i, false);
						}
					}
				}, new Action1<Throwable>() {
					@Override
					public void call(Throwable throwable) {
						LogUtils.i("异常-->"+throwable.toString());
					}
				});
	}
	private void notLikeNews(String tagid){
		Map<String, String> map = new HashMap();
		map.put("nid", nid);
		map.put("device", DeviceUtils.getMyUUID(activity));
		map.put("tagid", tagid);
		if (spu.getUser()!=null){
			map.put("uid",spu.getUser().getUid());
		}
		Factory.provideHttpService().newsLoseInterest(map)
				.subscribeOn(Schedulers.io())
				.observeOn(AndroidSchedulers.mainThread())
				.subscribe(new Action1<BaseEntity>() {
					@Override
					public void call(BaseEntity entity) {
						LogUtils.i("notLike-->"+entity.code);
//						DelNewsEvent event = new DelNewsEvent();
//						ventBus.getDefault().post(event);
					}
				}, new Action1<Throwable>() {
					@Override
					public void call(Throwable throwable) {
						LogUtils.i("异常-->"+throwable.toString());
					}
				});
	}
	public boolean getChoiceBoolean(int position) {
		if (null != choiceMap) {
			return choiceMap.get(position);
		}
		return false;
	}
	public static void setListViewHeightBasedOnChildren(GridView listView) {
	    // 获取listview的adapter  
	       ListAdapter listAdapter = listView.getAdapter();  
	       if (listAdapter == null) {  
	           return;  
	       }  
	       // 固定列宽，有多少列  
	       int col = 3;// listView.getNumColumns();  
	       int totalHeight = 0;  
	       // i每次加4，相当于listAdapter.getCount()小于等于4时 循环一次，计算一次item的高度，  
	       // listAdapter.getCount()小于等于8时计算两次高度相加  
	       for (int i = 0; i < listAdapter.getCount(); i += col) {  
	        // 获取listview的每一个item  
	           View listItem = listAdapter.getView(i, null, listView);  
	           listItem.measure(5, 5);  
	           // 获取item的高度和  
	           totalHeight += listItem.getMeasuredHeight();  
	           LogUtils.i("grid item hight-->"+listItem.getMeasuredHeight());
	       }  
	       
	       // 获取listview的布局参数  
	       ViewGroup.LayoutParams params = listView.getLayoutParams();  
	       // 设置高度  
	       params.height = totalHeight + listView.getHeight()*(adapter.getCount()-1)+10;  
	       LogUtils.i("grid view -->"+ totalHeight);
	       // 设置margin  
//	       ((MarginLayoutParams) params).setMargins(10, 10, 10, 10);  
	       // 设置参数  
//	       listView.setLayoutParams(params);  
	   }

	@Override
	public void onClick(View view) {
//		for (int i = 0; i < choiceMap.size(); i++) {
//			if (choiceMap.get(i)) {
//				select.add(list.get(i).getTagid());
//			}
//		}
//		LogUtils.i(select.toString());
//		StringBuilder sb=new StringBuilder();
//		if(null!=select){
//			for(String s:select){
//				sb.append(s);
//				sb.append(",");
//			}
//			if(sb.length()>1){
//				selectList=sb.substring(0, sb.length()-1);
//			}
//		}
		notLikeNews("0");
		dismiss();
	}
}
